<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="/WEB-INF/tlds/spring.tld"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<c:set var="BasePath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="Plugins" value="${pageContext.request.contextPath}/res/plugins"></c:set>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新增多渠道订单</title>
<link href="${Plugins}/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="${Plugins}/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/jquery/jquery-1.3.2.min.js"	type="text/javascript"></script>
<script src="${Plugins}/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.form.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.validate.method.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/ligerCommon.js"></script>
<script src="${Plugins}/datepicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/json2.js"></script>

<style type="text/css">
    body{ font-size:12px;}
    .l-table-edit {}
    .l-table-edit-td{ padding:4px;}
    .l-button-submit,.l-button-test{width:80px; float:left; margin-left:10px; padding-bottom:2px;}
    .l-verify-tip{ left:230px; top:120px;}
    .navbar {
		height: 20px;
		border-bottom: #E6E6E6 solid 1px;
		line-height: 20px;
	}
	.p-tab .s-lab{font-size:12px;font-family:"微软雅黑",​"宋体",​Arial,​sans-serif;}
	input.l-textarea{width:170px;height:20px;font-size:12px;}
</style>
</head>
<body style="padding:10px">
<div class="navbar">
		<span>您当前的位置：</span>
		多渠道订单管理 &gt; 新增多渠道订单管理
</div>
<form name="form1" method="post" action="add_purchaseReturn.do" id="form1" onsubmit="return setGridData()">
<input type="text" id="gridData" name="defectives" value="" style="display:none;"/> 
   <table  cellpadding="0" cellspacing="0" class="l-table-edit p-tab"" >
    <tr>
        <td align="right" class="l-table-edit-td s-lab"></td>
        <td align="left" width="100%" class="l-table-edit-td "></td>
        <td align="left"></td>
    </tr>
   
   
   <tr>
        <td align="right" class="l-table-edit-td s-lab" >订单渠道：</td>
        <td align="left" class="l-table-edit-td "> 
    		<select style="z-index: -999;font-size: 12px;" id="channelOrderType" name="channelOrderType" class="l-textarea">
				<option value="0">京东</option>
				<option value="1">淘宝</option>
				<option value="2">天猫</option>
				<option value="3">自营</option>
			</select>
		</td>
		<td align="left"></td>
    </tr>
   
   <%--  <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>订单渠道：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="channelOrderType" type="text" id="channelOrderType" value="${order.channelOrderType }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
    </tr> --%>
    
    <tr>
        <td align="right" class="l-table-edit-td s-lab" >订单状态：</td>
        <td align="left" class="l-table-edit-td "> 
    		<select style="z-index: -999;font-size: 12px;" id="orderState" name="orderState" class="l-textarea">
				<option value="0">已提交</option>
				<option value="1">审核通过</option>
				<option value="2">审核不通过</option>
				<option value="3">待支付</option>
				<option value="4">待发货</option>
				<option value="5">已发货</option>
				<option value="6">已完成</option>
				<option value="7">已取消</option>
				<option value="8">已锁定</option>
				<option value="9">部分发货</option>
				<option value="10">已收货</option>
				<option value="11">已关闭</option>
				<option value="12">已生成包裹单</option>
				<option value="13">已生成出库单</option>
			</select>
		</td>
		<td align="left"></td>
    </tr>
    
    <%--  <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>订单状态：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="orderState" type="text" id="orderState" value="${order.orderState }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr> --%>
     
     <tr>
        <td align="right" class="l-table-edit-td s-lab" >发货状态：</td>
        <td align="left" class="l-table-edit-td "> 
    		<select style="z-index: -999;font-size: 12px;" id="deliveryState" name="deliveryState" class="l-textarea">
				<option value="0">未发货</option>
				<option value="1">已发货</option>
			</select>
		</td>
		<td align="left"></td>
    </tr>
     
    <%--  <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>发货状态：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="deliveryState" type="text" id="deliveryState" value="${order.deliveryState }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr> --%>
     
      <tr>
        <td align="right" class="l-table-edit-td s-lab" >支付状态：</td>
        <td align="left" class="l-table-edit-td "> 
    		<select style="z-index: -999;font-size: 12px;" id="payState" name="payState" class="l-textarea">
				<option value="0">未支付</option>
				<option value="1">已支付</option>
			</select>
		</td>
		<td align="left"></td>
    </tr>
    
      <%-- <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>支付状态：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="payState" type="text" id="payState" value="${order.payState }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr> --%>
     
      <tr>
        <td align="right" class="l-table-edit-td s-lab" >支付方式：</td>
        <td align="left" class="l-table-edit-td "> 
    		<select style="z-index: -999;font-size: 12px;" id="payType" name="payType" class="l-textarea">
				<option value="0">货到付款</option>
				<option value="1">邮局汇款</option>
				<option value="2">自提</option>
				<option value="3">在线支付</option>
				<option value="4">公司转账</option>
				<option value="5">银行卡转账</option>
			</select>
		</td>
		<td align="left"></td>
    </tr>
    <%--  <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>支付方式：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="payType" type="text" id="payType" value="${order.payType }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr> --%>
     
     <tr>
        <td align="right" class="l-table-edit-td s-lab" >订单类型：</td>
        <td align="left" class="l-table-edit-td "> 
    		<select style="z-index: -999;font-size: 12px;" id="orderType" name="orderType" class="l-textarea">
				<option value="0">白拿</option>
				<option value="1">返还</option>
				<option value="2">天猫</option>
			</select>
		</td>
		<td align="left"></td>
    </tr>
    <%--<tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>订单类型：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="orderType" type="text" id="orderType" value="${order.orderType }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr> --%>
     
     <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>订单总金额：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="orderTotalPrice" type="text" id="orderTotalPrice" value="${order.orderTotalPrice }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr>
     
     <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>收货订单标记：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="returnOrder" type="text" id="returnOrder" value="${order.returnOrder }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr>
     
     <%-- <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>退货状态：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="returnState" type="text" id="returnState" value="${order.returnState }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr> --%>
     
      <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>备注：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="orderRemark" type="text" id="orderRemark" value="${order.orderRemark }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr>
     
      <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>收货人姓名：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="receiverName" type="text" id="receiverName" value="${order.receiverName }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
     </tr>
     
      <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>收货人电话：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="receiverPhone" type="text" id="receiverPhone" value="${order.receiverPhone }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
      </tr>
    
   <!-- 收货人信息 -->
     <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>收货人姓名：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="fullname" type="text" id="fullname" value="${prv.fullname }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
    </tr>
     <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>收货人座机：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="telephone" type="text" id="telephone" value="${prv.telephone }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
    </tr>
    <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>收货人手机：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="mobile" type="text" id="mobile" value="${prv.mobile }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
    </tr>
    <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>省：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="province" type="text" id="province" value="${prv.province }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
    </tr>
     <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>市：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="city" type="text" id="city" value="${prv.city }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
    </tr>
     <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>区：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="county" type="text" id="county" value="${prv.county }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
    </tr>
    <tr>
        <td align="right" class="l-table-edit-td s-lab"><font  color="red">*</font>地址详情：</td>
        <td align="left" width="100%" class="l-table-edit-td "><input name="fullAddress" type="text" id="fullAddress" value="${prv.fullAddress }" class="l-textarea" maxlength="50" validate="{required:true,rangelength:[1,50]}" /></td>
        <td align="left"></td>
    </tr>
     
     <tr>
        <td align="right" class="l-table-edit-td s-lab">
        	<input type="button" value="选择货品" id="elect_product" class="l-button l-button-test" />
        </td>
        <td align="left" class="l-table-edit-td ">
        	<input type="hidden" id="productlist" name="productlist" />
        	<!-- <input type="button" onclick="valData();" value="ssss"  /> -->
        </td>
        <td align="left"></td>
     </tr>
      <tr id="xzProductList" style="display: none;">
     <tr>
        <td colspan="3" align="left" class="l-table-edit-td s-lab">
        <div id="maingrid" style="margin:0; padding:0"></div>
		  <div style="display:none;">
		  <!-- 展示列表数据  -->
		</div>
		</td>
     </tr>

</table>


<input type="submit" value="确定" id="submit1" class="l-button l-button-submit" /> 
<input type="button" value="取消" name="tests" id="tests" class="l-button l-button-test" />
</form>
</body>
<script>

var basePath ="${BasePath}";
$(document).ready(function(){

	//选择商品子窗口
	$("#elect_product").click(function(){
		list = new Array();
	   	var m =$.ligerDialog.open({
	   				title:'采购货品',
	   				url:'${BasePath}/order/channelOrder/getCommodityList.do',
	   				height:500,width:700,
	   				buttons: [{ text: '确定', onclick: function (item, dialog) { 
	   									dialog.frame.valProd();
	   									if(!dialog.frame.isOut) return false;
	   									addProduct(); 
	   									dialog.close(); 
	   								} 
	   						  },
	   				          { text: '取消', onclick: function (item, dialog) { dialog.close(); list = new Array();} } ],
	   				isResize: false, 
	   				isDrag: false 
	   		});
	   	m.show(); 
	});
	
	addProduct();
});


//查询
var sel = "";
//临时请购商品集合
var list = new Array();
//请购商品集合
var commoditylist = new Array();
var g = "";
//请购商品列表
function addProduct(){
	commoditylist = commoditylist.concat(list);
	//$.ligerDialog.warn(JSON.stringify(commoditylist));
	commodityData = {"Rows": commoditylist, "Total": commoditylist.length};
	grid = $("#maingrid").ligerGrid({
        columns: [
        { display: '货品编号', name: 'productNo' ,width: '10%' ,render:function(rowdata,rowindex,value){
       	 rowdata.productNo = rowdata.productNo; 
          return rowdata.productNo; 
       }},
       { display: '商品名称', name: 'ppName'  ,width: '30%',render:function(rowdata,rowindex,value){
      	 rowdata.ppName = rowdata.ppName; 
         return rowdata.ppName; 
      }},
         { display: '数量', name: 'yieldAmountNumner' ,width: '10%',editor:{type:'int'},render:function(rowdata,rowindex,value){
       		var a = rowdata.yieldAmountNumner > rowdata.maxYieldAmountNumner;
       		if(a){
       			return rowdata.maxYieldAmountNumner;
       			}else if(rowdata.yieldAmountNumner<=0){
       				return rowdata.maxYieldAmountNumner;
       			}
       		return rowdata.yieldAmountNumner;
         
      }},
         { display: '退货总金额', name: 'totalPrice' ,width: '7%',render:function(rowdata,rowindex,value){
        	 if(rowdata.yieldAmountNumner != null && rowdata.ppUnitPrice != null){
     			var p = rowdata.yieldAmountNumner * rowdata.ppUnitPrice;
         		var num = new Number(p);
         		value = num.toFixed(2);
         		commoditylist[rowindex].totalPrice = value;
     		}
     		if(value == null || value == "") value ="0.00";
     		return value;
         }},
         { display: '退货成本价', name: 'ppUnitPrice' ,width: '10%',render:function(rowdata,rowindex,value){
         	 rowdata.ppUnitPrice = rowdata.ppUnitPrice; 
            return rowdata.ppUnitPrice; 
         }},
         { display: '备注', name: 'description',width: '10%',editor:{type:'string'},render:function(rowdata,rowindex,value){
           	 rowdata.description = value; 
             return value; }},
        { display: '操作', isSort: false,width: '10%', render: function (rowdata, rowindex, value){
           	var  text = "<input type='button' value='删除' onclick='removeThis("+rowindex+");' />";
            return text;
        	}
        }
        ], 
        usePager: false,enabledEdit: true,
        data: $.extend(true,{},commodityData)
    });
	list = new Array();
	//
	  g= $("#maingrid").ligerGrid({
		         grid: grid,
	 			pageSize:30,
	            width: '100%',
	            height: '92%',
	            dataType: 'server', 
	            dataAction: 'server',
	            pagesizeParmName: 'pageSize',
	            enabledEdit: true
	           /*  onBeforeSubmitEdit : onBeforeSubmitEdit */
	        });
	        
	//
}
	 function setGridData(){
	 		var data = g.getData();
	 		 //校验数量并重新计算总价、以免误差
	 		 $.each(data,function(index,item){
	 			 if(item.yieldAmountNumner!=null && item.ppUnitPrice!=null){
	 				 data[index].totalPrice=item.yieldAmountNumner * item.ppUnitPrice;
	 			 }else{
	 				 $.ligerDialog.warn("良品数或者单价不能为空!");
	 					return false;
	 			 }
	 		 }); 
	 		var dataStr = JSON.stringify (data);
	 		$('#gridData').val(dataStr);
	 		return true;
	}

//删除商品
function removeThis(o) {
	commoditylist.splice(o,1);
	addProduct();
}


///货品列表数据验证
var prod = true;
function valData() {
	debugger
	prod = true;
	var header = {'ppNumber': '数量','ppUnitPrice': '供应商','purWhNo': '仓库'};
	
	 $.each(commoditylist,function(index,item){
		if(item.returnDesc == 0){
			$.ligerDialog.warn("退货原因不能为空!");
			prod = false;
			return false;
		}
		if(!prod) return prod;
	}); 
	 
	 $.each(commoditylist,function(index,item){
			if(item.description == 0){
				$.ligerDialog.warn("退货备注不能为空!");
				prod = false;
				return false;
			}
			if(!prod) return prod;
		}); 
	 
	 $.each(commoditylist,function(index,item){
			if(item.yieldAmountNumner == 0){
				$.ligerDialog.warn("退货数量不能为空!");
				prod = false;
				return false;
			}else if(item.yieldAmountNumner > (item.ppNumber-item.defectiveAmountNumber) ){
				$.ligerDialog.warn("退货数量不能大于良品数量!");
				prod = false;
				return false;
			}
		
			if(!prod) return prod;
		}); 
	
	$("#productlist").val(JSON.stringify(commoditylist));
	//$.ligerDialog.warn(JSON.stringify(commoditylist));
}



//供应商编码
var scode ="";
//供应商名称
var sname="";
//成本价格
var actprice="";
//from提交
$(function ()
		{
			
		    $.metadata.setType("attr", "validate");
		    var v = $("form").validate({
		        errorPlacement: function (lable, element)
		        {
		        	lable.ligerHideTip();
		            if (element.hasClass("l-textarea"))
		            {
		                element.ligerTip({ content: lable.html(), target: element[0] }); 
		            }
		            else if (element.hasClass("l-text-field"))
		            {
		                element.parent().ligerTip({ content: lable.html(), target: element[0] });
		            }
		            else
		            {
		                lable.appendTo(element.parents("td:first").next("td"));
		            }
		            
		        },
		        submitHandler:function()
		        {
		        	$("form .l-text,.l-textarea").ligerHideTip();
		        	  /*  valData();
		        	if(!prod) return prod;    */
		            var options = {
		            		type: 'POST',
		            		beforeSend: function(){
		            			$("#form1").find(":submit,:button").attr("disabled", true);
		            		},
		            		success: function(data){
		            			if(data=="success") {
		            				window.parent.f_tip('新增成功');
		            				close_liger_tab();
		            			} else {
		            				window.parent.f_tip('新增失败');
		            			}
		            		}
		            	}, $form1 = $("#form1");
		           $form1.ajaxSubmit(options);
		           return false;
		        }
		    });
}); 
// 取消
$("#tests").click(function() {
	close_liger_tab();
});	


//控制良品
function onBeforeSubmitEdit(e){ 
	debugger;
	var record = e.record;
	var column = record.ppNumber;//退换货总数量
	var deletnum = record.defectiveAmountNumber;//次品总数量
	var yield = column-deletnum;//良品总数量
	var editValue = record.yieldAmountNumner;
		if(column.name == "yieldAmountNumner"){
			var prodTotal = record.ppNumber;
			if(editValue > yield){
				e.value = yield;
				window.parent.f_tip('输入的良品数量不能超过原有的良品数量!');
			}else if(editValue < 0){
				e.value = yield;
				window.parent.f_tip('请输入不小于0的数');
			}else if(editValue = 0){
				e.value = yield;
				window.parent.f_tip('请输入不等于0');
			}else{
				e.record.yieldAmountNumner = editValue;
			}
		  }
        }
</script>
</html>